/* -----------------------------------------------------------------------
 * Demonstration of how to write unit tests for dominion-base
 * Include the following lines in your makefile:
 *
 * unittest3: unittest3.c dominion.o rngs.o
 *      gcc -o unittest3 -g  unittest3.c dominion.o rngs.o $(CFLAGS)
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include "interface.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include "rngs.h"

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 0

int main() {
	//uncomment these 2 lines and comment out the next 2 for a specific seed
    //int seed = 1000;
	//srand(seed);
	int seed;
	srand(time(NULL));
	int maxTests = 100;
    int numPlayer = 2;
    int p, r, tests, first, second, third;
    int result;
	char buff1[20];
	char buff2[20];
	char buff3[20];
    /*
	int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    */
	int k[NUM_K_CARDS];
	struct gameState G;
    
	//gives weird message at end
	seed = rand() % 2000;
	printf ("\nStarting test isGameOver(). Number of tests: %d # players: %d Seed: %d\n", maxTests, numPlayer, seed);
	selectKingdomCards(rand(), k);	//pick random kingdom cards
#if (NOISY_TEST == 1)
    printKingdomCards(k);
#endif
	//tests
	//set province supply to 0 and run assert
	//set any three supplies (other than province) to 0 and run assert
	
    for (p = 0; p < numPlayer; p++)
    {
        for (tests = 1; tests <= maxTests; tests++)
        {
            
            //not sure this is necessary
			memset(&G, 23, sizeof(struct gameState));   // clear the game state
            r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
         
			G.supplyCount[province] = 0;				//set province to 0
			result = isGameOver(&G);					//check if game is over
#if (NOISY_TEST == 1)
    printf("\nProvince is empty. isGameOver = %d, expected = 1\n", result);
#endif
            assert(result == 1); 			// check if isGameOver is correct
				
			G.supplyCount[province] = 1;	//make sure first test doesn't brake the rest
			
			while(1){
				first = rand() % 25;
				second = rand() % 25;
				third = rand() % 25;
				if(first != second && first != third && second != third){
					//printf("breaking");
					break;
				}
				//printf("checking");
			}
				
			G.supplyCount[first] = 0;
			G.supplyCount[second] = 0;
			G.supplyCount[third] = 0;
			result = isGameOver(&G);
#if (NOISY_TEST == 1)
	cardNumToName(first, buff1);
	cardNumToName(second, buff2);
	cardNumToName(third, buff3);
    printf("%s, %s, and %s are empty. isGameOver = %d, expected = 1\n", buff1, buff2, buff3, result);
#endif
            assert(result == 1); 			//check if isGameOver is correct

			if(first == 3){
				G.supplyCount[first] = 1;		//make game not over
			}else if(second == 3){
				G.supplyCount[second] = 1;		//make game not over
			}else{
				G.supplyCount[third] = 1;		//make game not over
			}
			result = isGameOver(&G);
#if (NOISY_TEST == 1)
	cardNumToName(first, buff1);
	cardNumToName(second, buff2);
    printf("%s and %s are empty. isGameOver = %d, expected = 0\n", buff1, buff2, result);
#endif
            assert(result == 0);				//check if isGameOver is correct
            
        }
    }

    printf("\tAll isGameOver() tests completer successfully...\n");

    return 0;
}
